﻿using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.IO;
using System.Transactions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NPMonitor.DataModel;
using NPMonitor.Implementation.Internal;

namespace NPMonitor.Library.Tests.Store
{
    [TestClass]
    public class DbStoreContextTest
    {
        [TestMethod]
        public void TransactionTest()
        {
            //Scenario s2 = new Scenario()
            //{
            //    JobId = 1234,
            //    ScenarioName = "Test",
            //    ScenarioNO = System.Guid.NewGuid().ToString(),
            //    URL = "http://wicresoft.com",
            //    Description = "Just test"
            //};

            try
            {
                //using (var ctx = new DbStoreContext())
                //{
                //    ctx.Entry(s2).State = System.Data.EntityState.Added;

                //    ctx.SaveChanges();
                //}

                using (var ctx = new DbStoreContext())
                {
                    //IDbConnection conn = ctx.Database.Connection;
                    //conn.Open();
                    //IDbTransaction trans = conn.BeginTransaction();
                    //conn.Close();

                    Scenario s1 = new Scenario()
                    {
                        JobId = 123,
                        ScenarioName = "Test",
                        ScenarioNO = System.Guid.NewGuid().ToString(),
                        URL = "http://wicresoft.com",
                        Description = "Just test"
                    };

                    ctx.Scenarios.Add(s1);
                    ctx.SaveChanges();

                    using (var trans = new TransactionScope())
                    {
                        ctx.Entry(s1).State = System.Data.EntityState.Added;

                        // string guid = System.Guid.NewGuid().ToString();
                        // File.Create(@"C:\tmp\test-" + guid + ".txt");

                        var s = ctx.Scenarios.Where(t => t.JobId == 1234).FirstOrDefault();
                        s.ScenarioName = "test 1234";
                        ctx.Entry(s).State = EntityState.Modified;

                        //int a = 0;
                        //int b = 1 / a;

                        ctx.SaveChanges();
                        trans.Complete();
                    }

                        //trans.Commit();
                }
            }
            catch
            {
                using (var ctx = new DbStoreContext())
                {
                    //Assert.IsTrue(ctx.Scenarios.Where(t => t.ScenarioNO == s2.ScenarioNO).Count() > 0);
                    //Assert.IsTrue(ctx.Scenarios.Where(t => t.ScenarioNO == s1.ScenarioNO).Count() == 0);
                }
            }
            finally
            {
                //using (var ctx = new DbStoreContext())
                //{
                //    IList<Scenario> scenarios = ctx.Scenarios.Where(t => t.ScenarioNO == s2.ScenarioNO || t.ScenarioNO == s1.ScenarioNO).ToList();

                //    scenarios.All((s) =>
                //    {
                //        ctx.Entry(s).State = EntityState.Deleted;
                //        ctx.Scenarios.Remove(s);
                //        return true;
                //    });

                //    ctx.SaveChanges();
                //}
            }
        }
    }
}
